package org.xxpay.pay.channel.payeasepay.demo;

import org.xxpay.pay.channel.payeasepay.demo.CFCA.MerchantX509Cert;
import org.xxpay.pay.channel.payeasepay.util.HttpUtil;
import org.xxpay.pay.channel.payeasepay.util.SignUtil;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class Payment_submit {
    private static final String SINGLE_CHECK_URL = "https://paytest.yizhifubj.com/prs/user_payment.checkit";

    public static void main(String[] args) throws Exception {
        //String XML = submit(); //

        System.out.println("-----------------------------------------------------");

        String pfxFile = "D:/certificate/payease/19482_private.pfx";//商户私钥文件
        String pfxPassword = "payN6886";//私钥键入密码
        String aliasPassword = "payN6886";//
        String aliasName = "{ae87b4b1-262d-4005-8cdc-9b7530f7cc21}";//别名
        String publicKey = "D:/certificate/payease/payease_cfca.cer";
        String signSrc = "20181217-19482-P01201812170135451770004200.010";
        String s = "9ab207ee577b0cf8ee847899c882d7b78eceebe4f8357f4b36fa40d66e51633aac5b7f815fec629404f7d33e4c6ae5eb050e06d49c2a84fd1bdf27bfb831a3178fc30578b8d8ecf547e259e03b75d7740ec344e3b381995def99b8442db1a7b45e1e433c53f9f09c4aff54773a9ea0c1738c27ebf28950e00a64ae7336be08b6b1b980f15cb86313c44be4ba8c6d929c6f6bb9e628b585370bbbbb371ec8679a14f10124f41f0dbd28cd9e8f329b0b202061c8af22073cd7bbdf86b5b25c7d6fcf3d381b4e4b4747cfe8014c78116efc3513b73e4744dc30fbda9f97c9d106dfc4312b11b096ee9191b067cf538aa2d5babfdd925c87c3372d59e527ad3273c2";

        String sign = MerchantX509Cert.sign(signSrc, pfxFile, aliasName, pfxPassword, aliasPassword);
        Boolean result = MerchantX509Cert.verifySign(signSrc,s,publicKey);
        System.out.println(s);
        System.out.println(sign);
        System.out.println(result);
    }

    public static String submit() throws Exception {

        String payHost = "http://127.0.0.1:3020";

        Map<String, Object> queryParams = new HashMap<>();

        String v_mid = "7606";
        String v_oid = "20181214-7606-12347";
        String v_moneytype = "0";
        String v_ymd = "20181214";
        String v_amount = "1.00";
        String v_rcvname = "7606";
        String v_rcvpost = "7606";
        String v_url = "http://4md8tj.natappfree.cc/prs/user_payment.html";
        String v_rcvaddr = "7606";
        String v_rcvtel = "7606";
        String v_orderstatus = "1";
        String v_ordername = "7606";

        /*//MD5加密
        com.capinfo.crypt.Md5 md5 = new com.capinfo.crypt.Md5("");
        //v_moneytype v_ymd v_amount v_oid v_mid v_branch v_url
        md5.hmac_Md5(v_moneytype + v_ymd + v_amount + v_rcvname + v_oid + v_mid + v_url, "test");//第一个参数是加密参数，第二个参数是加密密钥，测试密钥：test，正式上线之前注意修改
        byte b[]= md5.getDigest();
        String v_md5info = md5.stringify(b);//订单数字指纹
        System.out.println("MD5签名：" + v_md5info);*/
        String url = SINGLE_CHECK_URL;

        String pfxFile = "D:/certificate/payease/cfca_private.pfx";//商户私钥文件
        String pfxPassword = "lh121121";//私钥键入密码
        String aliasPassword = "lh121121";//
        String aliasName = "{3dfcbb2d-bb3d-4350-9010-33de8cdb076c}";//别名
        String src = v_moneytype + v_ymd + v_amount + v_rcvname + v_oid + v_mid + v_url;
        System.out.println("加密前的拼串：" + src);
        String v_md5info = MerchantX509Cert.sign(src, pfxFile, aliasName, pfxPassword, aliasPassword);
        String v_md5info2 = MerchantX509Cert.sign(src, pfxFile, aliasName, pfxPassword, aliasPassword);
        System.out.println("CFCA签名：" + v_md5info);
        System.out.println("CFCA签名：" + v_md5info2);

        queryParams.put("v_mid", v_mid);
        queryParams.put("v_oid", v_oid);
        queryParams.put("v_moneytype", v_moneytype);
        queryParams.put("v_ymd", v_ymd);
        queryParams.put("v_amount", v_amount);
        queryParams.put("v_rcvname", v_rcvname);
        queryParams.put("v_rcvpost", v_rcvpost);
        queryParams.put("v_url", v_url);
        queryParams.put("v_rcvaddr", v_rcvaddr);
        queryParams.put("v_rcvtel", v_rcvtel);
        queryParams.put("v_orderstatus", v_orderstatus);
        queryParams.put("v_ordername", v_ordername);
        queryParams.put("v_md5info", v_md5info);

        queryParams.put("mchId", 20000000 + "");  //商户ID
        queryParams.put("appId", "");  //商户应用ID
        queryParams.put("productId", 8000);  //支付产品ID
        queryParams.put("mchOrderNo", System.currentTimeMillis() + "");   //商户订单号
        queryParams.put("currency", "cny");   //币种
        queryParams.put("amount", 10);   //支付金额
        queryParams.put("clientIp", "210.73.10.148");   //客户端IP
        queryParams.put("device", "ios10.3.1");   //客户端设备
        queryParams.put("returnUrl", "http://localhost:8080/return_page.html");   //支付结果前端跳转URL
        queryParams.put("notifyUrl", "http://localhost:8080/api/pay/notify");   //支付结果后台回调URL
        queryParams.put("subject", "网络购物");  //商品主题
        queryParams.put("body", "网络购物");   //商品描述信息
        queryParams.put("param1", "");   //扩展参数1
        queryParams.put("param2", "");   //扩展参数2
        queryParams.put("extra", "");  //附加参数

        String sign = SignUtil.getSign(queryParams, "D4SZ8TQK1Z8UPYMOLSKQQPMWYKVXW8IAHBMNJEFXJLCYPF7AWKCTKN1SXWS82ZNPMOBRFEGCK5TOGOQKPC59LP0FHIP6TU5GZ5TZXHHJ7YDGHSWP2URHZX1YUKPUMPAM");  //签名
        queryParams.put("sign", sign);

        queryParams.put("mchId", "20000000");
        queryParams.put("productId", "8000");

        /*StringBuffer payForm = new StringBuffer();
        payForm.append("<form id=\"test\" name=\"test\" action=\""+url+"\" method=\"post\">");
        for(String k : queryParams.keySet()) {
            payForm.append("<input type=\"hidden\" name=\""+k+"\" id=\""+k+"\" value=\""+queryParams.get(k)+"\">");
        }
        payForm.append("<input type=\"submit\" value=\"立即支付\" style=\"display:none\" >");
        payForm.append("</form>");
        payForm.append("<script>document.forms[0].submit();</script>");*/

        HttpUtil.post(payHost + "/api/pay/create_order", genUrlParams(queryParams));

        return null;
    }

    /**
     * map 转换为  url参数
     * <p><b>Description: </b>
     * <p>2018年9月30日 上午10:12:33
     * @param paraMap
     * @return
     */
    private static String genUrlParams(Map<String, Object> paraMap) {
        if(paraMap == null || paraMap.isEmpty()) return "";
        StringBuffer urlParam = new StringBuffer();
        Set<String> keySet = paraMap.keySet();
        int i = 0;
        for(String key:keySet) {
            urlParam.append(key).append("=").append(paraMap.get(key));
            if(++i == keySet.size()) break;
            urlParam.append("&");
        }
        return urlParam.toString();
    }

}
